package com.zxb.base.monitor.vo;

import cn.hutool.core.util.NumberUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zxb.base.monitor.util.FormatUtil;
import com.zxb.base.monitor.util.LayoutUtil;
import lombok.Builder;
import lombok.Data;
import lombok.experimental.Accessors;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @author Zxb
 */
@Data
@Builder
@Accessors(chain = true)
public class Memory {
    /**
     * 总物理内存
     */
    private long totalPhysicalMemory;

    /**
     * 空闲物理内存
     */
    private long freePhysicalMemory;

    /**
     * 已使用物理内存
     */
    private long usedPhysicalMemory;

    /**
     * 物理内存使用率
     */
    private BigDecimal physicalMemoryUsage;

    /**
     * 总内存
     */
    private long totalMemory;

    /**
     * 空闲内存
     */
    private long freeMemory;

    /**
     * 已用内存
     */
    private long usedMemory;

    /**
     * 最大内存
     */
    private long maxMemory;

    /**
     * 最大可用内存
     */
    private long maxUseMemory;

    /**
     * 内存使用率
     */
    private BigDecimal memoryUsage;

    @Override
    public String toString() {
        return LayoutUtil.Table.of(
                LayoutUtil.Row.of("Total Physical Memory", FormatUtil.formatByteSize(totalPhysicalMemory)),
                LayoutUtil.Row.of("Free Physical Memory", FormatUtil.formatByteSize(freePhysicalMemory)),
                LayoutUtil.Row.of("Used Physical Memory", FormatUtil.formatByteSize(usedPhysicalMemory)),
                LayoutUtil.Row.of("Physical Memory Usage", NumberUtil.mul(physicalMemoryUsage, 100).setScale(2, RoundingMode.HALF_UP) + "%"),
                LayoutUtil.Row.of("Total Memory", FormatUtil.formatByteSize(totalMemory)),
                LayoutUtil.Row.of("Free Memory", FormatUtil.formatByteSize(freeMemory)),
                LayoutUtil.Row.of("Used Memory", FormatUtil.formatByteSize(usedMemory)),
                LayoutUtil.Row.of("Max Memory", FormatUtil.formatByteSize(maxMemory)),
                LayoutUtil.Row.of("Max Use Memory", FormatUtil.formatByteSize(maxUseMemory)),
                LayoutUtil.Row.of("Memory Usage", NumberUtil.mul(memoryUsage, 100).setScale(2, RoundingMode.HALF_UP) + "%")
        ).toString();
    }

    public JSONArray toJSON() {
        JSONArray ja = new JSONArray();
        JSONObject jo = new JSONObject();
        jo.put("totalPhysicalMemory", FormatUtil.formatByteSize(totalPhysicalMemory));
        jo.put("freePhysicalMemory", FormatUtil.formatByteSize(freePhysicalMemory));
        jo.put("usedPhysicalMemory", FormatUtil.formatByteSize(usedPhysicalMemory));
        jo.put("physicalMemoryUsage", NumberUtil.mul(physicalMemoryUsage, 100).setScale(2, RoundingMode.HALF_UP) + "%");
        jo.put("totalMemory", FormatUtil.formatByteSize(totalMemory));
        jo.put("freeMemory", FormatUtil.formatByteSize(freeMemory));
        jo.put("usedMemory", FormatUtil.formatByteSize(usedMemory));
        jo.put("maxMemory", FormatUtil.formatByteSize(maxMemory));
        jo.put("maxUseMemory", FormatUtil.formatByteSize(maxUseMemory));
        jo.put("memoryUsage", NumberUtil.mul(memoryUsage, 100).setScale(2, RoundingMode.HALF_UP) + "%");
        ja.add(jo);
        return ja;
    }
}
